/*
 * Copyright (C), 2014-2016, org.esaydev
 * FileName: WechatServlet.java
 * Author:   jian
 * Date:     2016年3月14日 下午5:11:06
 */
package org.easydev.wechat.servlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.easydev.wechat.common.util.HttpUtil;
import org.easydev.wechat.common.util.SignUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <Description>微信servlet <br>
 * 
 * @author jian<br>
 * @version 1.0<br>
 */
public class WechatServlet extends HttpServlet {

    /**
     * serialVersionUID <br>
     */
    private static final long serialVersionUID = 8653780187860037615L;

    /**
     * LOGGER
     */
    private final static Logger LOGGER = LoggerFactory.getLogger(WechatServlet.class);

    private static String serviceName = StringUtils.EMPTY;

    /**
     * 微信请求处理
     */
    private static AbsWechatService wechatService;

    /**
     * get wechatService
     * 
     * @return Returns the wechatService.<br>
     */
    public static AbsWechatService getWechatService() {
        return wechatService;
    }

    /**
     * set wechatService
     * 
     * @param wechatService The wechatService to set. <br>
     */
    public static void setWechatService(AbsWechatService wechatService) {
        WechatServlet.wechatService = wechatService;
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        String signature = request.getParameter("signature");
        String timestamp = request.getParameter("timestamp");
        String nonce = request.getParameter("nonce");
        String echostr = request.getParameter("echostr");
        LOGGER.info("[wechat servlet get req] signature:{} ; timestamp:{},nonce: {}; echostr: {}", new Object[] {
                signature, timestamp, nonce, echostr
        });
        if (null != signature && null != timestamp && nonce != null && SignUtil.checkSignature(signature, timestamp, nonce)) {
            HttpUtil.write(echostr, response);
        } else {
            HttpUtil.write("Parameter lost or signature error!", response);
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) {
        if (null != wechatService) {
            HttpUtil.write(wechatService.processRequest(request, response), response);
        } else {
            if (StringUtils.isNotBlank(serviceName)) {
                try {
                    Class<?> cls = Class.forName(serviceName);
                    wechatService = (AbsWechatService) cls.newInstance();
                } catch (ClassNotFoundException e) {
                    LOGGER.error("ClassNotFoundException ", e);
                } catch (InstantiationException e) {
                    LOGGER.error("InstantiationException ", e);
                } catch (IllegalAccessException e) {
                    LOGGER.error("IllegalAccessException ", e);
                }
            }
            if (null == wechatService) {
                HttpUtil.write("Service is not avilable now! Please try again later!", response);
            } else {
                HttpUtil.write(wechatService.processRequest(request, response), response);
            }
        }
    }

}
